package com.example.rwq_test.ThreadPool;

import java.util.concurrent.*;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2024/4/11
 */
public class MyRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//        try {
            System.out.println("线程池满,当前线程池数量:" + executor.getQueue().size() +" 当前任务信息:"+ r.getClass());
//            // 放回对垒重新执行
//            executor.getQueue().put(r);
//        } catch (InterruptedException e1) {
            //将异常抛出
            throw new RejectedExecutionException("ThreadPoolExecutor Queue full.");
//        }
    }

    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(3, 5, 10,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(7), new MyThreadFactory() , new MyRejectedExecutionHandler());

        for (int i = 0; i < 100; i++) {
            int finalI = i;
            executorPool.execute(() -> {
                System.out.println(finalI);
                System.out.println("currentThreadName: " + Thread.currentThread().getName());
            });
        }
    }
}
